package com.axxc.jpa.task.rabbitmsg.receiver;

import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.ObjectOptimisticLockingFailureException;
import org.springframework.stereotype.Component;

import com.axxc.jpa.entity.TestPersonEntity;
import com.axxc.jpa.repository.ITestPersonEntityRepository;

/**
 * @ClassName: RabbitMqReceiver
 * @Description: 消息消费者A 与 消息消费者B 分别均匀消费 队列(string_msg_queue) 中的消息
 *               多生产者一样,生产到相同的队列,分别均匀消费
 * @author xxc
 * @date 2018年8月3日 下午3:33:38
 * @see http://47.98.248.215:15672/#/
 */
@Component
@RabbitListener(queues = "test_person")
public class RabbitMqTestPersonEntityMsgReceiverA {

	@Autowired
	private ITestPersonEntityRepository testPersonEntityRepository;

	@RabbitHandler
	public void processA(TestPersonEntity u) {
		// this.testPersonEntityRepository.save(u);
		for (int i = 0; i < 5; i++) { // 模拟并发操作
			new Thread(new Runnable() {
				@Override
				public void run() {
					try {
						testPersonEntityRepository.save(u);
					} catch (ObjectOptimisticLockingFailureException e) {// 捕获并发修改的异常
						System.out.println("修改失败,版本号不正确");
					}
				}
			}).start();
		}
	}
}
